﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.Diagnostics.Management;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace FaceSharp.MVC
{
    public class WebRole : RoleEntryPoint
    {
        public override bool OnStart()
        {
            ConfigureDiagnostics();

            // For information on handling configuration changes
            // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.

            return base.OnStart();
        }

        private void ConfigureDiagnostics()
        {
            //More Details : http://msdn.microsoft.com/en-us/library/ee843890.aspx

            String wadConnectionString = "Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString";

            CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(wadConnectionString));

            DeploymentDiagnosticManager deploymentDiagnosticManager =
                new DeploymentDiagnosticManager(
                    cloudStorageAccount,
                    RoleEnvironment.DeploymentId);

            RoleInstanceDiagnosticManager roleInstanceDiagnosticManager =
                cloudStorageAccount.CreateRoleInstanceDiagnosticManager(
                    RoleEnvironment.DeploymentId,
                    RoleEnvironment.CurrentRoleInstance.Role.Name,
                    RoleEnvironment.CurrentRoleInstance.Id);

            DiagnosticMonitorConfiguration diagnosticMonitorConfiguration =
                roleInstanceDiagnosticManager.GetCurrentConfiguration();


            // Windows Event Logs
            diagnosticMonitorConfiguration.WindowsEventLog.DataSources.Add("Application!*");
            diagnosticMonitorConfiguration.WindowsEventLog.DataSources.Add("System!*");
            diagnosticMonitorConfiguration.WindowsEventLog.ScheduledTransferPeriod = TimeSpan.FromMinutes(5.0d);
            diagnosticMonitorConfiguration.WindowsEventLog.ScheduledTransferLogLevelFilter = LogLevel.Undefined;


            // Performance Counters
            // http://msdn.microsoft.com/en-us/library/fxk122b4.aspx

            List<string> counters = new List<string>();
            counters.Add(@"\Processor(_Total)\% Processor Time");
            counters.Add(@"\System\Processor Queue Length");
            counters.Add(@"\Process(_Total)\Thread Count");
            counters.Add(@"\Web Service\ISAPI Extension Requests/sec");
            counters.Add(@"\Memory\Available Mbytes");
            counters.Add(@"\Network Interface(*)\Bytes Received/sec");
            counters.Add(@"\Network Interface(*)\Bytes Sent/sec");
            counters.Add(@"\TCPv4\Connections Established");
            counters.Add(@"\ASP.NET Applications(__Total__)\Requests/Sec");
            counters.Add(@"\ASP.NET\Application Restarts");
            counters.Add(@"\ASP.NET\Requests Current");
            counters.Add(@"\ASP.NET\Requests Queued");
            counters.Add(@"\ASP.NET\Requests Rejected");
            counters.Add(@"\ASP.NET\Request Wait Time");

            foreach (var counter in counters)
            {
                PerformanceCounterConfiguration performanceCounterConfiguration = new PerformanceCounterConfiguration();
                performanceCounterConfiguration.CounterSpecifier = counter;
                performanceCounterConfiguration.SampleRate = TimeSpan.FromSeconds(10.0d);
                diagnosticMonitorConfiguration.PerformanceCounters.DataSources.Add(performanceCounterConfiguration);
            }

            diagnosticMonitorConfiguration.PerformanceCounters.ScheduledTransferPeriod = TimeSpan.FromMinutes(1.0d);


            // IIS Logs
            diagnosticMonitorConfiguration.Directories.ScheduledTransferPeriod = TimeSpan.FromMinutes(5.0d);


            // cannot be less than 1 minute otherwise will reset to zero and will not transfer files.
            diagnosticMonitorConfiguration.Logs.ScheduledTransferPeriod =
               TimeSpan.FromMinutes(1.0d);

            // Apply Settinggs
            roleInstanceDiagnosticManager.SetCurrentConfiguration(diagnosticMonitorConfiguration);

            // Start Monitoring
            DiagnosticMonitor.Start(wadConnectionString, diagnosticMonitorConfiguration);
        }

    }
}